package com.asia5b.a5mall.middleware.utils

import android.content.Context
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.model.shop.Goods2GalleryBean
import com.bumptech.glide.Glide
import com.github.iielse.imageviewer.ImageViewerBuilder
import com.github.iielse.imageviewer.adapter.ItemType
import com.github.iielse.imageviewer.core.DataProvider
import com.github.iielse.imageviewer.core.ImageLoader
import com.github.iielse.imageviewer.core.Photo
import com.github.iielse.imageviewer.core.Transformer

/**
 * Created by zhangyuncai on 2020/8/29.
 */
object ImageWatchHelper {

    fun <T> show(context: Context, clickView: View? = null, anyList: MutableList<T>, position: Long = 0) {
        val imageList = mutableListOf<ImageWatchPhoto>()
        anyList.forEachIndexed { index, any ->
            if (any is String) {
                imageList.add(ImageWatchPhoto(index.toLong(), any))
            } else if (any is Goods2GalleryBean) {
                imageList.add(ImageWatchPhoto(index.toLong(), "${any.big}"))
            }
        }
        if (imageList.isEmpty()) {
            return
        }
        val builder = ImageViewerBuilder(
                context = context,
                initKey = position,
                dataProvider = ImageWatchDataProvider(imageList),
                imageLoader = ImageWatchLoader2(),
                transformer = object : Transformer {
                    override fun getView(key: Long): ImageView? {
                        if (clickView == null) {
                            return null
                        }
                        return fakeStartView(clickView)
                    }
                }
        )
        builder.show()
    }
}

data class ImageWatchPhoto(val position: Long = 0, val url: String) : Photo {
    override fun id(): Long {
        return position
    }

    override fun itemType(): Int {
        return ItemType.PHOTO
    }
}


class ImageWatchLoader2 : ImageLoader {
    override fun load(view: ImageView, data: Photo, viewHolder: RecyclerView.ViewHolder) {
        val imageWatchPhoto = data as ImageWatchPhoto
        Glide.with(view.context)
                .load(imageWatchPhoto.url)
                .into(view)
    }
}

class ImageWatchDataProvider constructor(val imageList: MutableList<ImageWatchPhoto>) : DataProvider {
    override fun loadInitial(): List<Photo> {
        return imageList
    }
}

// 提供原图尺寸/位置/加载模式
private fun fakeStartView(view: View): ImageView? {
    val customWidth = view.width
    val customHeight = view.height
    val customLocation = IntArray(2).also { view.getLocationOnScreen(it) }
    val customScaleType = ImageView.ScaleType.CENTER_CROP
    return ImageView(view.context).apply {
        left = 0
        right = customWidth
        top = 0
        bottom = customHeight
        scaleType = customScaleType
        setTag(R.id.viewer_start_view_location_0, customLocation[0])
        setTag(R.id.viewer_start_view_location_1, customLocation[1])
    }
}